The MNIST database


The MNIST database

The Modified National Institute of Standards and Technology database has 60000 images with handwritten digits for training and 10000 images for testing (validation). The width of each image is 28 pixels and its height is 28 pixels.
Esta base de datos tiene 60000 imágenes con números de un solo dígito escritos a mano para entrenamiento y 10000 imágenes para prueba (validación). El ancho de cada imagen es 28 pixeles y su altura es 28 pixeles.

Problem 1
Write a summary with 400 words describing the MNIST database.
Escriba un resumen con 400 palabras describiendo la base de datos MNIST.

IDX file format

This format is used to store a multidimensional array. There are two common formats IDX1 and IDX3 as shown in the figure below. IDX1 stores a one dimensional array, while IDX3 stores a array of matrices of the same size. In an Intel processor, you must reverse the order of the bytes for each integer value at the top of the file.
Este formato es usado para almacenar arreglos multidimensionales. Hay dos formatos comunes IDX1 e IDX3 como se muestra en la figura. IDX almacena un arreglo de una dimensión, mientras que IDX3 almacena un arreglo de matrices del mismo tamaño. En un procesador de Intel, usted debe invertir el orden de los bytes para cada valor entero en la parte superior del archivo.

IDX

Problem 2
Download the four files of the MNIST database, http://yann.lecun.com/exdb/mnist/index.html. Each label in the dataset indicates the class that the image belongs.
  1. train-images-idx3-ubyte.gz: training set images (9912422 bytes)
  2. train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
  3. t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
  4. t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

Descargue los cuatro archivos de la base de datos MNIST, http://yann.lecun.com/exdb/mnist/index.html. Cada etiqueta en el conjunto de datos indica la clase a la que la imagen pertenece.
  1. train-images-idx3-ubyte.gz: conjunto de imágenes de entrenamiento (9912422 bytes)
  2. train-labels-idx1-ubyte.gz: conjunto de etiquetas de entrenamiento (28881 bytes)
  3. t10k-images-idx3-ubyte.gz: conjunto de imágenes de prueba (1648877 bytes)
  4. t10k-labels-idx1-ubyte.gz: conjunto de etiquetas de prueba (4542 bytes)

Problem 3
Briefly describe:
  1. The idx1 format used in the MNIST database (use 100 words)
  2. The idx3 format used in the MNIST database (use 100 words)

Describa brevemente:
  1. El formato idx1 usado en la base de datos MNIST (use 100 palabras)
  2. El formato idx3 usado en la base de datos MNIST (use 100 palabras)

Problem 4
Create a Neural Lab project called MNIST. Select the option of Main file only.
Cree un proyecto de Neural Lab llamado MNIST. Selecciona la opción de Main file only.

Step A
Rename the files of the MNIST database as shown below. Be sure to change the extension of the files to idx.gz. The gz extension means that the idx file is compressed using the Gzip compression algorithm.
  1. train-images-idx3-ubyte.gz to trainInput.gz
  2. train-labels-idx1-ubyte.gz to trainTarget.gz
  3. t10k-images-idx3-ubyte.gz to validInput.gz
  4. t10k-labels-idx1-ubyte.gz to validTarget.gz

Cambie el nombre a los archivos de la base de datos MNIST como se muestra debajo. Asegúrese de cambiar la extensión de los archivos a idx.gz. La extensión gz indica que el archivo idx está comprimido usando el algoritmo de compresión Gzip.
  1. train-images-idx3-ubyte.gz a trainInput.idx.gz
  2. train-labels-idx1-ubyte.gz a trainTarget.idx.gz
  3. t10k-images-idx3-ubyte.gz a validInput.idx.gz
  4. t10k-labels-idx1-ubyte.gz a validTarget.idx.gz

mnist_folder

Step B
Edit the Main.lab file as shown. Then, execute the code.
Edite el archivo Main.lab cómo se muestra. Entonces, ejecute el código.

MNIST\Main.lab
//_________________________________________ 1. Load trainInput
Tensor trainInput;
trainInput.LoadIdx();
//_________________________________________ 2. Load trainTarget
Tensor trainTarget;
trainTarget.LoadIdx();

trainInput

trainTarget

Problem 5
Search over the Internet of a classic configuration for a convolutional neural network for the MNIST database for digits.
Busque en la Internet una configuración clásica para una red neuronal convolucional para la base de datos MNIST de los dígitos.

Problem 6
In this exercise, we will build the basic convolutional neural network shown below to classify the digits of the MNIST database. Add a new file called InitWeights.lab. The number of neurons in the output layer must be 10, one for each class. However, the number of neurons in the layer before the output layer depends on the number of training cases that are used (if you use more training cases, you can use more neurons). Depending on your computer, this problem may take many hours to be completed, plan ahead and use overnight runs. We will save the network in the hard drive at several times so that we can continue training in the future.
En este ejercicio, nosotros construiremos la red neuronal convolucional básica mostrada debajo para clasificar los dígitos de la base de datos de MNIST. Agregue un archivo nuevo llamado InitWeights.lab. El número de neuronas en la capa de salida debe ser de 10, una para cada clase. Sin embargo, el número de neuronas en la capa previa a la capa de salida depende del número de casos de entrenamiento que se usan (si usted usa mas casos de entrenamiento, usted puede usar mas neuronas). Dependiendo se su computadora, este problema puede tardar muchas horas en completarse, planee con anticipación y use ejecuciones nocturnas. Nosotros almacenaremos la red en el disco duro en varias ocasiones de tal forma que podremos continuar en el entrenamiento en el futuro.

convPool1

MNIST\InitWeights.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(1875, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(1875, 59999);
//_______________________________________ 3. Create the network
ConvNet net;
net.Create(28, 28, 1, 4);
net.SetInRange(0.0, 255.0);
net.SetOutRange(0.0, 1.0);
//_______________________________________ 4. Layer setup
net.SetConvLayer(0, 1, 3, 0, 1, 1); // logsig=1, visualField=3, pad=0, stride=1, numFilters=1
net.SetPoolLayer(1, 5, 2, 2); // maximum=5, visualField=2, stride=2
net.SetFullLayer(2, 1, 70); // logsig=1, neurons=150
net.SetFullLayer(3, 1, 10); // logsig=1, 10 classes
net.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 5. Train using simulated annealing
net.TrainSimAnneal(
     20, //Number of Temperatures
     20, //Number Iterations
     15, //Initial Temperature
     0.001, //Final Temperature
     true, //Is Cooling Schedule Linear?
     4, //Number of Cycles
     1.0e-6, //Goal (desired mse)
     true //Use Singular Value Decomposition
);
net.Save();

Step A
After initializing the weights of the network, add a new file called Train1.lab to train the network. We will perform progressive training at five stages. At each stage, we will check the training, and then, we will incorporate more training cases. In the first step, Train1.lab, we will use 1875 training cases
Después de inicializar los pesos de la red, agregue un archivo nuevo llamado Train1.lab para entrenar la red convolucional. Nosotros realizaremos entrenamiento progresivo en cinco etapas. En cada etapa, verificaremos el entrenamiento, y entonces, incorporaremos más casos de entrenamiento. En el primer paso, Train1.lab, usaremos 1875 casos de entrenamiento.

MNIST\Train1.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(1875, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(1875, 59999);
//_______________________________________ 3. Load the network
ConvNet net;
net.Load();
net.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 4. Continue training
net.TrainConjGrad(1500,1.0e-10);
net.Save();

Step B
After training the network, add a new file called CheckTrain1.lab to check the training by computing the confusion matrix. As we can see, the training cases in the MNIST database are randomly distributed among the ten classes. Therefore, we can use progressive training without shuffling the cases in the training set.
Después de entrenar la red, agregue un archivo nuevo llamado CheckTrain1.lab para verificar el entrenamiento calculando la matriz de confusión. Cómo se puede ver, los casos de entrenamiento en la base de datos MNIST esta aleatoriamente distribuidas entre las diez clases. Por lo tanto, nosotros podemos usar entrenamiento progresivo sin revolver los casos en el conjunto de datos de entrenamiento.

MNIST\CheckTrain1.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(1875, 59999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(1875, 59999);
//_________________________________________ 3. Load the network
ConvNet net;
net.Load();
//_________________________________________ 4. Run
Tensor output;
net.Run(trainInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConf1

Tip
Remember than the training on step 1 is much more faster than the training on next steps. Therefore, if you think that the error can be further reduced during this step, you can again execute the program Train1.lab.
Recuerde que el entrenamiento en el paso 1 es mucho rápido que el entrenamiento en los pasos siguientes. Por lo tanto, si usted piensa que el error puede ser reducido más durante este paso, usted puede volver ejecutar el programa Train1.lab.

Problem 7
Add a new file called Train2.lab to train the network using 3750 cases. After the training has been completed, add a new file called CheckTrain2.lab to verify the training using 3750 cases. If you want to reduce the number of errors, you can run again the program Train2.lab.
Agregue un archivo nuevo llamado Train2.lab para entrenar la red usando 3750 casos. Después de que el entrenamiento se ha completado, agregue un nuevo archivo CheckTrain2.lab para verificar el entrenamiento usando 3750 casos. Si usted quiere reducir el número de errores, usted puede ejecutar el programa Train2.lab otra vez.

MNIST\Train2.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(3750, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(3750, 59999);
//_______________________________________ 3. Load the network
ConvNet net;
net.Load();
net.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 4. Continue training
net.TrainConjGrad(1500,1.0e-10);
net.Save();

MNIST\CheckTrain2.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(3750, 59999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(3750, 59999);
//_________________________________________ 3. Load the network
ConvNet net;
net.Load();
//_________________________________________ 4. Run
Tensor output;
net.Run(trainInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConf2

Problem 8
Add a new file called Train3.lab to train the network using 7500 cases. After the training has been completed, add a new file called CheckTrain3.lab to verify the training using 7500 cases. If you want to reduce the number of errors, you can run again the program Train3.lab.
Agregue un archivo nuevo llamado Train3.lab para entrenar la red usando 7500 casos. Después de que el entrenamiento se ha completado, agregue un nuevo archivo CheckTrain3.lab para verificar el entrenamiento usando 7500 casos. Si usted quiere reducir el número de errores, usted puede ejecutar el programa Train3.lab otra vez.

MNIST\Train3.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(7500, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(7500, 59999);
//_______________________________________ 3. Load the network
ConvNet net;
net.Load();
net.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 4. Continue training
net.TrainConjGrad(1500,1.0e-10);
net.Save();

MNIST\CheckTrain3.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(7500, 59999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(7500, 59999);
//_________________________________________ 3. Load the network
ConvNet net;
net.Load();
//_________________________________________ 4. Run
Tensor output;
net.Run(trainInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConf3

numErrors3

Problem 9
Add a new file called Train4.lab to train the network using 15000 cases. After the training has been completed, add a new file called CheckTrain4.lab to verify the training using 15000 cases.
Agregue un archivo nuevo llamado Train4.lab para entrenar la red usando 15000 casos. Después de que el entrenamiento se ha completado, agregue un nuevo archivo CheckTrain4.lab para verificar el entrenamiento usando 15000 casos.

MNIST\Train4.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(15000, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(15000, 59999);
//_______________________________________ 3. Load the network
ConvNet net;
net.Load();
net.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 4. Continue training
net.TrainConjGrad(1500,1.0e-10);
net.Save();

MNIST\CheckTrain4.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(15000, 59999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(15000, 59999);
//_________________________________________ 3. Load the network
ConvNet net;
net.Load();
//_________________________________________ 4. Run
Tensor output;
net.Run(trainInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConf4

numErrors4

Problem 10
Add a new file called TrainSoft.lab to train the network using the SoftMax activation function. Before running the program, copy the net.cnn file to netSoft.cnn. This problem will be used with 15000 training cases, however, you can use the 60000 cases if you have a powerful computer.
Agregue un archivo nuevo llamado TrainSoft.lab para entrenar la red usando la función de activación SoftMax. Antes de ejecutar el programa, copie el archivo net.cnn a netSoft.cnn. Este problema usará 15000 casos de entrenamiento, sin embargo, usted puede usar los 60000 casos si usted tiene una computadora poderosa.

MNIST\TrainSoft.lab
//_______________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(15000, 59999);
//_______________________________________ 2. Load the target (class for each digit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(15000, 59999);
//_______________________________________ 3. Load the network
ConvNet netSoft;
netSoft.Load();
netSoft.SetActiFunc(3, 4); //SoftMax=4
netSoft.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 4. Continue training
netSoft.TrainConjGrad(1500,1.0e-10);
netSoft.Save();

MNIST\CheckSoft.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(15000, 59999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
trainTarget.DeleteRange(15000, 59999);
//_________________________________________ 3. Load the network
ConvNet netSoft;
netSoft.Load();
//_________________________________________ 4. Run
Tensor output;
netSoft.Run(trainInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix trainConf;
output.ConfusionMatrix(trainTarget, 0.5, trainConf);
trainConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(trainConf.GetSum()) - toint(trainConf.GetDiagonalSum());

trainConfSoft

Tip
The figure below shows the results using the 60000 training cases.
La figura de abajo muestra los resultados usando los 60000 casos de entrenamiento.

MNIST\TrainFull.lab
//_______________________________________ 1. Network setup
ConvNet netFull;
netFull.Load();
//_______________________________________ 2. Load and set the training set
Tensor trainInput;
trainInput.LoadIdx();
Tensor trainTarget;
trainTarget.LoadIdx();
trainTarget.ExpandClass();
netFull.SetActiFunc(3, 4); //SoftMax=4
netFull.SetTrainSet(trainInput, trainTarget, false);
//_______________________________________ 3. Train Conjugate Gradient (logsig)
netFull.TrainConjGrad(1000, 1.0e-8);
netFull.Save();

trainConfFull

numErrFull

Problem 11
Add a new file called Validation.lab to validate the performance of the network by computing the confusion matrix.
Agregue un archivo nuevo llamado Validation.lab para verificar el desempeño de la red calculando la matriz de confusión.

MNIST\Validation.lab
//_________________________________________ 1. Load the input tensor
Tensor validInput;
validInput.LoadIdx();
validInput.DeleteRange(2500, 9999);
//_________________________________________ 2. Load the target (class for each fruit)
Tensor validTarget;
validTarget.LoadIdx();
validTarget.ExpandClass();
validTarget.DeleteRange(2500, 9999);
//_________________________________________ 3. Load the network
ConvNet netSoft;
netSoft.Load();
//_________________________________________ 4. Run
Tensor output;
netSoft.Run(validInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix validConf;
output.ConfusionMatrix(validTarget, 0.5, validConf);
validConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(validConf.GetSum()) - toint(validConf.GetDiagonalSum());

validConf

valNumErrors

Tip
The figure below shows the results using the 60000 training cases.
La figura de abajo muestra los resultados usando los 60000 casos de entrenamiento.

MNIST\ValidFull.lab
//_________________________________________ 1. Load the input tensor
Tensor validInput;
validInput.LoadIdx();
//_________________________________________ 2. Load the target (class for each fruit)
Tensor validTarget;
validTarget.LoadIdx();
validTarget.ExpandClass();
//_________________________________________ 3. Load the network
ConvNet netFull;
netFull.Load();
//_________________________________________ 4. Run
Tensor output;
netFull.Run(validInput, output);
//_________________________________________ 5. Compute the Confusion Matrix
Matrix validConf;
output.ConfusionMatrix(validTarget, 0.5, validConf);
validConf.Save();
//_________________________________________ Compute the Number of Errors
int numErrors = toint(validConf.GetSum()) - toint(validConf.GetDiagonalSum());

validConfFull

valNumErrFull

Problem 12
Add a new file called Activation.lab to inspect the activation of the convolutional layer.
Agregue un archivo nuevo llamado Activation.lab para inspeccionar la activación de la capa convolucional.

MNIST\Activation.lab
//_________________________________________ 1. Load the input tensor
Tensor trainInput;
trainInput.LoadIdx();
trainInput.DeleteRange(15000, 59999);
//_________________________________________ 2. Load the network
ConvNet net;
net.Load();
//_________________________________________ 3. Activation
Tensor output;
net.GetActivation(0, trainInput, output);

activation

Tip
A convolutional layer can have one, two or more filters. The network shown below has a convolutional layer with two filters.
Una capa convolucional puede tener uno, dos o tres filtros. La red mostrada debajo tiene una capa convolucional con dos filtros.

convPool2

Problem 13
  1. Do you recommend adding another filter to the convolutional layer?
  2. Do you recommend adding another convolutional layer to the network?
  3. Do you recommend adding another pool layer to the network?

  1. Recomienda usted agregar otro filtro a la capa convolucional?
  2. Recomienda agregar otra capa convolucional a la red?
  3. Recomienda agregar otra capa de pooling a la red?

Tip
Convolutional neural networks have many parameters that can influence the performance of the network. You can
  1. Use Simulated Annealing or Genetic Algorithm for the pre-training
  2. Enable or disable singular value decomposition
  3. Modify the parameter of Simulated Annealing or the Genetic Algorithm
  4. Try with different types of layers in the network

Las redes neuronales convolucionales tienen muchos parámetros que pueden afectar el desempeño de la red. Usted puede
  1. Usar Simulated Annealing o Algoritmos Genéticos para el pre-entrenamiento
  2. Habilitar o deshabilitar descomposición en valores singulares
  3. Modificar los parámetros de Simulated Annealing o del Algoritmo Genético
  4. Intentar diferentes tipos de capas en la red

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home